module WWW
  class Tkd
    class GwtSellerOrder
      @@LOG_FLAG="GwtSellerOrder"

      @@AGENT_YUANTONG    = 1#运通
      @@AGENT_SHENTONG    = 2#申通
      @@AGENT_EMS         = 3#EMS
      @@AGENT_SHUNFENG    = 4#顺风
      @@AGENT_YUNDA       = 5#韵达
      @@AGENT_HUITONG     = 6#汇通
      @@AGENT_TIANTIAN    = 7#天天
      @@AGENT_YAFENG      = 8#亚风
      @@AGENT_CCES        = 9#中诚快递
      @@AGENT_ZHONGTONG   = 10#中通
      @@AGENT_CHANGFA     = 11#长发
      @@AGENT_NENGDA      = 12#能达
      @@AGENT_QUANYI      = 13 #全一
      @@AGENT_EYOUBAO      = 14#E邮宝
      @@AGENT_ZAIJISONG      = 15#宅急送
      @@AGENT_FENGHUO      = 16#风火
      attr_reader :order,:express,:status
      attr_writer :order,:express,:status
      def to_json(*args)
        {
          'order'  => @order,
          'express'        => @express,
          'status'        => @status,
        }.to_json(*args)
      end
      #通过订单信息初始化其快递信息
      def self.init_by_order order,user_id
        view = self.new
        view.order = order
        seller_order = WWW::Tkd::TbSellerOrder.find_by_order_id_and_user_id order.id,user_id
        if seller_order==nil#发现尚未关联的就将express与order进行关联
          seller_order = WWW::Tkd::TbSellerOrder.new
          seller_order.order_id = order.id
          seller_order.user_id = user_id
          seller_order.is_newer = 0
          seller_order.save
        end
        view.express = WWW::Tkd::Express.find_by_id(seller_order.express_id)
        if view.express!=nil
          view.status = find_status(view.express)
        end
        return view
      end
      #获得卖家最近的订单与快递信息
      def self.get_last_order user_id
        view = self.new
        seller_order_list = Mi::Utils::Sql.select Mi::Model::Sql.last_seller_order user_id
        if seller_order_list!=nil && seller_order_list.size>0
          seller_order = seller_order_list[0]
          update_order = WWW::Tkd::TbSellerOrder.find_by_id seller_order["id"]
          update_order.is_newer=0
          update_order.save
          order = WWW::Tkd::TbOrder.find_by_id seller_order["order_id"]
          if order!=nil
            view.order=order
            view.express = WWW::Tkd::Express.find_by_id(seller_order["express_id"])
            if view.express!=nil
              view.status = find_status(view.express)
            end
          end
        end
        return view
      end
      def self.find_status express
        return WWW::Tkd::Status.find_form_agent_id_and_no(express.agent_id,express.no_code)
      end
      #将tb_express的快递信息与tb_order关联
      def self.save_from_tb_express tb_express
        agent_name = tb_express.company_name
        no_code = tb_express.out_sid
        if agent_name!=nil
          agent_id=get_agent_id_from_tb_express agent_name
          if(agent_id>0)
            #回置tb_express，标示该快递以及入库
            tb_express.is_converted = 1
            tb_express.save
            #将其与买家与卖家进行关联
            tid = tb_express.tid
            user_id = tb_express.user_id
            support_status  = 1
            if(agent_id>12)#标示该快递公司尚未支持
              support_status  = 0
            end
            if no_code==nil#标示快递号为空
              support_status = -1
            end
            seller_order = WWW::Tkd::TbSellerOrder.find_from_tid_and_user_id(tid,user_id)
            if seller_order!=nil
              save_form_tb_express(seller_order,agent_id,no_code,support_status,user_id)
              WWW::Tkd::Logger.debug  "#{@@LOG_FLAG}seller_auto_relate_express","order_id:#{seller_order.id},agent_id:#{agent_id},code:#{no_code}"
            else
              WWW::Tkd::Logger.debug  "#{@@LOG_FLAG}seller_auto_relate_express_failed","tid:#{tid}"
            end
          end
        end
      end
      #根据agent_name获得系统的快递公司标示
      def self.get_agent_id_from_tb_express agent_name
        if agent_name=='EMS'
          return @@AGENT_EMS
        end
        if agent_name=='e邮宝'
          return @@AGENT_EYOUBAO
        end
        if agent_name=='中通速递'
          return @@AGENT_ZHONGTONG
        end
        if agent_name=='全一'
          return @@AGENT_QUANYI
        end
        if agent_name=='圆通速递'
          return @@AGENT_YUANTONG
        end
        if agent_name=='天天快递'
          return @@AGENT_TIANTIAN
        end
        if agent_name=='宅急送'
          return @@AGENT_ZAIJISONG
        end
        if agent_name=='汇通'
          return @@AGENT_HUITONG
        end
        if agent_name=='申通E物流'
          return @@AGENT_SHENTONG
        end
        if agent_name=='韵达快运'
          return @@AGENT_YUNDA
        end
        if agent_name=='顺丰'
          return @@AGENT_SHUNFENG
        end
        if agent_name=='风火天地'
          return @@AGENT_FENGHUO
        end
        return -1
      end
      #保存订单信息
      def self.save_form_tb_express seller_order,agent_id,no_code,support_status,user_id
        #确认该记录是该用户的记录
        db_express = WWW::Tkd::Express.find_by_agent_id_and_no_code(agent_id,no_code)
        #新建express对象
        if db_express==nil
          db_express= WWW::Tkd::Express.new
          db_express.agent_id=agent_id
          db_express.no_code=no_code
          db_express.support_status = support_status
          db_express.is_new=0
          db_express.not_exist=0
          db_express.is_sync=0
          db_express.is_end=0
          db_express.save
        end
        #更新tb_seller_orders对象
        if seller_order!=nil
          ori_express_id = seller_order.express_id
          if ori_express_id!=db_express.id#只有快遞更新後才會更改
            seller_order.is_newer = true
            seller_order.express_id = db_express.id
            seller_order.save
          end
        end
        #更新tb_buyer_orders对象
        order = WWW::Tkd::TbOrder.find_by_id seller_order.order_id
        if order!=nil
          buyer = WWW::Tkd::TbUser.find_by_domain_login_id order.buyer_nick
          if buyer!=nil
            buyer_order = WWW::Tkd::TbBuyerOrder.find_by_order_id_and_user_id order.id,buyer.id
            if buyer_order!=nil
              ori_express_id = buyer_order.express_id
              if ori_express_id!=db_express.id#只有快遞更新後才會更改
                buyer_order.is_newer = true
                buyer_order.express_id = db_express.id
                buyer_order.save
              end
            end
          end
        end
      end
      #保存订单信息,来源用户选择并输入的快递信息，这样的快递当然支持的
      def self.save order,express,user_id
        save_form_tb_express order,express.agent_id,express.no_code,1,user_id
      end
    end
  end
end
